package com.example.utils;


import com.example.dao.StudentDao;
import com.example.entity.Student;
import com.example.servlet.LoginServlet;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;
import java.io.IOException;

@WebFilter("/*")
public class AutoLoginFilter implements Filter {
    private StudentDao studentDao = new StudentDao();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化过滤器
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpSession session = req.getSession();

        //获取请求路径
        String url = req.getRequestURI();
        System.out.println(url);

        String requestURI = req.getRequestURI();
        String contextPath = req.getContextPath();
        String path = requestURI.substring(contextPath.length());

        // 静态资源直接放行
        if (path.startsWith("/css/") ||
                path.startsWith("/js/") ||
                path.endsWith(".css") ||
                path.endsWith(".js")
               ) {
            chain.doFilter(request, response);
            return;
        }

        // 登录相关页面直接放行
        if (path.equals("/login.jsp") || path.equals("/login")) {
            chain.doFilter(request, response);
            return;
        }
        // 登录相关页面直接放行
        if (path.equals("/register.jsp") || path.equals("/register")) {
            chain.doFilter(request, response);
            return;
        }
        if (session.getAttribute("student") != null) {
            System.out.println("用户已登录，用户名：" + session.getAttribute("student")); // 添加日志
            chain.doFilter(request, response);
            return;
        }

        // 检查自动登录cookie

        Cookie[] cookies = req.getCookies();
        System.out.println("检查自动登录cookie");
        if (cookies != null) {
            String name = null;
            String email = null;
            System.out.println("cookies不为空");
            for (Cookie cookie : cookies) {
                System.out.println("cookie的值"+cookie);
                if ("remember".equals(cookie.getName())) {
                    name = cookie.getValue();
                    System.out.println("自动登录"+name);
                    break;
                }
                if ("remember".equals(cookie.getName())) {
                    email = cookie.getValue();
                    System.out.println("自动登录"+email);
                    break;
                }
            }
            Student student = studentDao.validateStudent(name, email);
            if (name != null && email != null) {
                if (student != null) {
                    // 验证成功，将用户信息存入session
                    session.setAttribute("student", student.getName());
                    System.out.println("自动登录成功");
                }
            }
        }
            // 如果不是访问登录页面，则重定向到登录页面
            if (!url.contains("login.jsp")) {
                resp.sendRedirect(req.getContextPath() + "/login.jsp");
                return;
            }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // 销毁过滤器
    }
}